<?php 
defined('IN_DESTOON') or exit('Access Denied');
$m_str = $MODULE[2]['linkurl'].$DT['file_my'];
// echo '<br>member->inc->index->11111111->userid'.$_userid.' str-->'.$m_str.'<br>';
if(!$_userid) dheader($MODULE[2]['linkurl'].$DT['file_my']); 

require DT_ROOT.'/module/'.$module.'/common.inc.php';
if($action == 'logout' && $admin_user) {
	set_cookie('admin_user', '');
	dmsg($L['index_msg_logout'], $MODULE[2]['linkurl']);
}
require MD_ROOT.'/member.class.php';
require DT_ROOT.'/include/post.func.php';
include load('search.lang');

// session_start([
//    'cookie_lifetime' => 86400,
//    'read_and_close'  => true,
// ]);

// echo '<br>member->inc->index->22222222222';
$do = new member;
if($submit) {
	echo '<br>member->inc->index->33333333';
	if(word_count($note) > 5000) message($L['index_msg_note_limit']);
	$note = '<?php exit;?>'.dhtmlspecialchars(stripslashes($note));
	file_put(DT_ROOT.'/file/user/'.dalloc($_userid).'/'.$_userid.'/note.php', $note);
	dmsg($L['op_update_success'], $MODULE[2]['linkurl']);
} 

else {
	$head_title = '';
	$do->userid = $_userid;
	$user = $do->get_one();
	extract($user);
	$expired = $totime && $totime < $DT_TIME ? true : false;
	$havedays = $expired ? 0 : ceil(($totime-$DT_TIME)/86400);

	$sys = array();
	$i = 0;
	$result = $db->query("SELECT itemid,title,addtime,groupids FROM {$DT_PRE}message WHERE groupids<>'' ORDER BY itemid DESC", 'CACHE');
	while($r = $db->fetch_array($result)) {
		$groupids = explode(',', $r['groupids']);
		if(!in_array($_groupid, $groupids)) continue;
		if($i > 2) continue;
		$i++;
		$sys[] = $r;
	}
	$note = DT_ROOT.'/file/user/'.dalloc($_userid).'/'.$_userid.'/note.php';
	$note = file_get($note);
	if($note) {
		$note = substr($note, 13);
	} else {
		$note = $MOD['usernote'];
	}

	$trade = $db->count("{$DT_PRE}mall_order", "seller='$_username' AND status=0");
	$t = explode('.', $_money);
	$my_money = $t[0].'<span>.'.$t[1].'</span>';


       ///////////////////////////////////////
       // areas
       $user_areas = array();
       $t_areas2 = explode("," , $user['zp_filteraddress']);
       foreach($t_areas2 as $area1 ) {
           if( !empty($area1) )
               $user_areas[] = $area1;
       }
       $iPos =  mb_strpos ( $user['zp_filteraddress'] , '(0)' );
       if( empty($user_areas) || $iPos !== false )
       {
           $AREA = cache_read('area.php');
           $user_areas = array();
           foreach($AREA as $area ) {
               if( $area['parentid'] == '0' )
                   $user_areas[] = $area;
           }
       }
       else
       {
           $area = array();
           foreach( $user_areas as $t )
           {
               $iFirst = mb_strpos($t, '(');
               $iSecond = mb_strpos($t, ')');
               $idArea = mb_substr($t, $iFirst + 1, $iSecond - $iFirst - 1);
               $nameArea = mb_substr($t, 0, $iFirst );
               $area[] = array( 'areaid'=>$idArea, 'areaname'=>$nameArea );
           }

           $user_areas = $area;
       }

       $search_areaid= $_GET['areaid'];
	  
       if( empty($search_areaid))
       {
           if( !isset($_SESSION['areaid']))
           {
               $search_areaid = "(0)";
           }
           else
           {
               $search_areaid = $_SESSION['areaid'];
           }
       }
       $_SESSION['areaid'] = $search_areaid;

       $search_areatext = '';
       foreach($user_areas as &$t1)
       {
           $searchText = "(" . $t1['areaid'] . ")";

           $t1['checked'] = '';
           if( !empty($search_areaid))
           {
               $iPos =  mb_strpos ( $search_areaid , $searchText  );
               if( $iPos  !== false )
               {
                   $t1['checked'] = 'checked';
                   $search_areatext .= $searchText . ",";
               }
               else
               {
                   $t1['checked'] = '';
               }
           }/*
           else
           {
               $t1['checked'] = 'checked';
               $search_areatext .=  $searchText . ",";
           }
           */
       }
       $searchText = "(0)";
       $area0_checked = "";
       $iPos =  mb_strpos ( $search_areaid, $searchText  );
       if( $iPos  !== false )
       {
           $area0_checked = 'checked="checked"';
       }

       /////////////////////////////////
       // filter categories
       $user_fcategories = array();
       $t_fcategories2 = explode("," ,  $user['zp_filtercategory']);
       foreach( $t_fcategories2 as $category1 ) {
           if( !empty($category1) )
               $user_fcategories[] = $category1;
       }
       $iPos =  mb_strpos ( $user['zp_filtercategory'] , '(0)' );

       // echo '<br>member->inc->index->444444444';

       if( empty($user_fcategories) || $iPos !== false )
       {
//       		echo '<br>member->inc->index->5555555';
           $catef = $db->query("SELECT fid as id, f_cat_name as cat_name, 1 as ischeck, 0 as ishide, f_cat_lg FROM {$DT_PRE}filtercategory WHERE f_cat_sm=0 order by displayorder asc");
           $fenleis = array();
           while($cf = $db->fetch_array($catef)){
               
               $catez = $db->query("SELECT fid as id, f_cat_name as cat_name, 1 as ischeck, 0 as ishide FROM {$DT_PRE}filtercategory WHERE f_cat_sm!=0 and f_cat_lg=" . $cf['f_cat_lg'] . " order by displayorder asc");
               while($cz = $db->fetch_array($catez)){
                   $cf['child'][] = $cz;
               }
                
               $fenleis[] = $cf;
           }
           $user_fcategories = $fenleis;
       }
       else
       {
       	// echo '<br>member->inc->index->666666666';
           $result = $db->query("SELECT fid as id, f_cat_name as cat_name, 1 as ischeck, 1 as ishide, f_cat_lg  FROM {$DT_PRE}filtercategory WHERE f_cat_sm=0 order by displayorder asc");
           $fenleis = array();
           while($cf = $db->fetch_array($result)){
               $catez = $db->query("SELECT fid as id, f_cat_name as cat_name, 1 as ischeck, 1 as ishide FROM {$DT_PRE}filtercategory WHERE f_cat_sm!=0 and f_cat_lg=" . $cf['f_cat_lg'] . " order by displayorder asc");
               while($cz = $db->fetch_array($catez)){
                   $cf['child'][] = $cz;
               }
               $fenleis[] = $cf;
           }

           // check categories
           $catef = array();
           foreach( $user_fcategories as $t )
           {
               $iFirst = mb_strpos($t, '(');
               $iSecond = mb_strpos($t, ')');
               $idCategory = mb_substr($t, $iFirst + 1, $iSecond - $iFirst - 1);

               foreach( $fenleis as &$fenlei )
               {
                   if( $fenlei['id'] == $idCategory )
                   {
                       $fenlei['ishide'] = 0;
                       foreach( $fenlei['child'] as &$cf )
                       {
                           $cf['ishide'] = 0;
                       }
                       continue;
                   }
                   else
                   {
                       foreach( $fenlei['child'] as &$cf )
                       {
                           if( $cf['id'] == $idCategory )
                               $cf['ishide'] = 0;
                       }
                   }
               }
           }

           // complete fenleis
           $complete_fenleis = array();
           foreach( $fenleis as &$fenlei )
           {
               if(  $fenlei['ishide'] == 0 )
               {
                   $complete_fenleis[] = $fenlei;
               }
               else
               {
                   $childs = array();
                   foreach( $fenlei['child'] as &$cf )
                   {
                       if( $cf['ishide'] == 0 )
                           $childs[] = $cf;
                   }
                   if( !empty($childs) )
                   {
                       $complete_fenleis[] = array('id'=>$fenlei['id']
                               , 'cat_name'=>$fenlei['cat_name']
                               , 'is_check'=>0
                               , 'is_hide'=>0
                               , 'child'=>$childs   );
                   }
               }
           }

           $user_fcategories = $complete_fenleis;
       }

       $search_filtercategory= $category;
       if( empty($search_filtercategory))
       {
           if( !isset($_SESSION['category']))
           {
               $search_filtercategory = "(0)";
           }
           else
           {
               $search_filtercategory = $_SESSION['category'];
           }
       }


       $_SESSION['category'] = $search_filtercategory;

       $search_filtercategorytext = '';
       foreach($user_fcategories as &$t3)
       {
           $t3_checked = false;
           /*
           foreach($t3['child'] as &$t4)
           {
               $searchText = "(" . $t4['id'] . ")";
               $t4['checked'] = '';
               if( !empty($search_filtercategory))
               {
                   $iPos =  mb_strpos ( $search_filtercategory , $searchText  );
                   if( $iPos  !== false )
                   {
                       $t4['checked'] = 'checked';
                       $t3_checked = true;
                       $search_filtercategorytext .= $searchText . ",";
                   }
                   else
                   {
                       $t4['checked'] = '';
                   }
               }
               else
               {
                   $t4['checked'] = 'checked';
                   $search_filtercategorytext .=  $searchText . ",";
                   $t3_checked = true;
               }
           }
           */
           if( $t3_checked == true )
           {
               $searchText = "(" . $t3['id'] . ")";
               $search_filtercategorytext .=  $searchText . ",";
               $t3['checked'] = 'checked';
           }
           else
           {
               $searchText = "(" . $t3['id'] . ")";
               $t3['checked'] = '';
               if( !empty($search_filtercategory))
               {
                   $iPos =  mb_strpos ( $search_filtercategory , $searchText  );
                   if( $iPos  !== false )
                   {
                       $t3['checked'] = 'checked';
                       $search_filtercategorytext .= $searchText . ",";
                       $t3_checked = true;
                   }
                   else
                   {
                       $t3['checked'] = '';
                   }
               }/*
               else
               {
                   $t3['checked'] = 'checked';
                   $search_filtercategorytext .=  $searchText . ",";
               }*/
           }
       }

       $searchText = "(0)";
       $fcategory0_checked = "";
       $iPos =  mb_strpos ( $search_filtercategory, $searchText  );
       if( $iPos  !== false )
       {
           $fcategory0_checked = 'checked="checked"';
       }

       ////////////////////////////
       // categorys
       $user_categorys = array();
       $t_categories2 = explode("," , $user['zp_category']);
       foreach( $t_categories2 as $category22 ) {
           if( !empty($category22) )
               $user_categorys[] = $category22;
       }

       // $user_categorys = explode("," , $user['zp_category']);
       $iPos =  mb_strpos ( $user['zp_category'] , '(0)' );
       if( empty($user_categorys) || $iPos !== false )
       {
          $user_categorys = $TOPCAT ;
       }
       else
       {
           $categories = array();
           foreach( $user_categorys as $t )
           {
               $iFirst = mb_strpos($t, '(');
               $iSecond = mb_strpos($t, ')');
               $idCategory = mb_substr($t, $iFirst + 1, $iSecond - $iFirst - 1);
               $nameCategory = mb_substr($t, 0, $iFirst );
               $categories[] = array( 'catid'=>$idCategory, 'name'=>$nameCategory );
           }

           $user_categorys = $categories;
       }

       


       $search_zpcategory= $zpcategory;
        if( empty($search_zpcategory))
        {
           if( !isset($_SESSION['zpcategory']))
           {
               $search_zpcategory = "(0)";
           }
           else
           {
               $search_zpcategory = $_SESSION['zpcategory'];
           }
        }

       $_SESSION['zpcategory'] = $search_zpcategory;

       $search_zpcategorytext = '';
       foreach($user_categorys as &$t2)
       {
           $searchText = "(" . $t2['catid'] . ")";

           $t2['checked'] = '';
           if( !empty($search_zpcategory))
           {
               $iPos =  mb_strpos ( $search_zpcategory , $searchText  );
               if( $iPos  !== false )
               {
                   $t2['checked'] = 'checked';
                   $search_zpcategorytext .= $searchText . ",";
               }
               else
               {
                   $t2['checked'] = '';
               }
           }
           /*
           else
           {
               $t2['checked'] = 'checked';
               $search_zpcategorytext .=  $searchText . ",";
           }
            */
       }


       $searchText = "(0)";
       $category0_checked = "";
       $iPos =  mb_strpos ( $search_zpcategory, $searchText  );
       if( $iPos  !== false )
       {
           $category0_checked = 'checked="checked"';
       }

       $condition = ' 1 ';


       $kw = $kw;
       
	   if(empty($kw))
       {
           if( isset($_SESSION['kw']))
           {
               if(!isset($kw)){
                  $kw = $_SESSION['kw'];
               }else{
                   $kw = '';
               }
           }else{
               $kw = $_SESSION['kw'];
           }
       }
       $_SESSION['kw'] = $kw;


       $_fields = $fields;
       if(empty($_fields))
       {
           if( !isset($_SESSION['fields']))
           {
               $_fields = "";
           }
           else
           {
               $_fields = $_SESSION['fields'];
           }
       }
       $_SESSION['fields'] = $_fields;


       if( !empty($kw) )
       {
           if( $_fields == 0 )
           {
                $condition .= " AND ( title LIKE '%". $kw ."%' OR keyword LIKE '%". $kw ."%') ";
           }
           else if( $_fields == 1 )
           {
                $condition .= " AND ( title LIKE '%". $kw ."%') ";
           }
           else if( $_fields == 2 )
           {
                $condition .= " AND ( d.content LIKE '%". $kw ."%') ";
           }
       }

       $t_areas = "";
       $t_filters = "";
       $t_categories = "";
       if( !empty($search_areatext) )
           $t_areas = explode( "," , $search_areatext );
       if( !empty($search_filtercategorytext) )
           $t_filters = explode( "," , $search_filtercategorytext );
       if( !empty($search_zpcategorytext) )
           $t_categories = explode( "," , $search_zpcategorytext );

       if( !empty( $t_areas ) )
       {
           $condition .= " AND (  LENGTH(filteraddress) = 0 OR filteraddress LIKE '%(0)%' OR ";
           $first = true;

           foreach( $t_areas as $t_area )
           {
               if( !empty( $t_area) )
               {
                   if( $first == false )
                       $condition .= " OR ";

                   $condition .= " filteraddress LIKE '%". $t_area ."%' ";
                   $first = false;
               }
           }
           $condition .= " ) ";
       }
       else {
           $condition .= " AND (  LENGTH(filteraddress) = 0 OR filteraddress LIKE '%(0)%' OR ";
           $first = true;

           foreach( $user_areas as $t_area )
           {
               if( !empty( $t_area) )
               {
                   if( $first == false )
                       $condition .= " OR ";

                   $condition .= " filteraddress LIKE '%(". $t_area['areaid'] .")%' ";
                   $first = false;
               }
           }
           $condition .= " ) ";
       }



       if( !empty( $t_filters ) )
       {
           $condition .= " AND (  LENGTH(filtercategory) = 0 OR filtercategory LIKE '%(0)%' OR ";
           $first = true;

           foreach( $t_filters as $t_filter )
           {
               if( !empty( $t_filter ) )
               {
                   if( $first == false )
                       $condition .= " OR ";

                   $condition .= " filtercategory LIKE '%". $t_filter ."%' ";
                   $first = false;
               }
           }
           $condition .= " ) ";
       }
       else {
           $condition .= " AND (  LENGTH(filtercategory) = 0 OR filtercategory LIKE '%(0)%' OR ";
           $first = true;

           foreach( $user_fcategories as $t_ucf )
           {
               if( !empty( $t_ucf) )
               {
                   if( $first == false )
                       $condition .= " OR ";

                   $condition .= " filtercategory LIKE '%(". $t_ucf['id'] .")%' ";
                   $first = false;
               }
           }
           $condition .= " ) ";
       }

       if( !empty( $t_categories ) )
       {
           $condition .= " AND ( ";
           $first = true;

           foreach( $t_categories as $t_category )
           {
               if( !empty( $t_category ) )
               {
                   if( $first == false )
                       $condition .= " OR ";

                   $replacestr = array("(", ")" );
                   $t_category = str_replace($replacestr, "", $t_category);

                   $condition .= " catid = " . $t_category . " ";
                   $first = false;
               }
           }
           $condition .= " ) ";
       }
        else {
           $condition .= " AND ( ";
           $first = true;

           foreach( $user_categorys as $t_cat2 )
           {
               if( !empty( $t_cat2) )
               {
                   if( $first == false )
                       $condition .= " OR ";

                   $condition .= " catid = ". $t_cat2['catid'] ." ";
                   $first = false;
               }
           }
           $condition .= " ) ";
       }

// echo '<br>member->inc->index->2777777777';

       $pagesize = 15;
	$offset = ($page-1)*$pagesize;
	$items = $db->count( "zb_article_23 i INNER JOIN zb_article_data_23 d ON d.itemid = i.itemid ", $condition );
	$pages = pages($items, $page, $pagesize);
       $zhaobiao_url = $MODULE[23]['linkurl']; // zhaobiao
       $datetype = 5;
       $showpage = true;

       if($items) {
           $order = " ORDER BY addtime desc  ";
           $fields = "i.itemid as itemid,title,thumb,linkurl,style,catid,introduce,addtime,edittime,username,islink,areaid,filteraddress, filtercategory";
           $result = $db->query("SELECT {$fields}, d.content as content FROM zb_article_23 i INNER JOIN zb_article_data_23 d ON d.itemid = i.itemid  WHERE {$condition}{$order} LIMIT {$offset},{$pagesize}");

           $index = 1;
           while($r = $db->fetch_array($result)) {
                   $r['adddate'] = timetodate($r['addtime'], 5);
                   $r['editdate'] = timetodate($r['edittime'], 5);
                   if($lazy && isset($r['thumb']) && $r['thumb']) $r['thumb'] = DT_SKIN.'image/lazy.gif" original="'.$r['thumb'];
                   $r['alt'] = $r['title'];
                   $r['title'] = set_style($r['title'], $r['style']);
                   if($kw) $r['title'] = str_replace($replacef, $replacet, $r['title']);
                   if(!$r['islink']) $r['linkurl'] = $zhaobiao_url.$r['linkurl'];
                   $r['itemno'] = $index + $offset;
                   $index = $index + 1;
                   $tags[] = $r;
           }
           $db->free_result($result);
	}

	include template('index', $module);
}

// echo '<br>member->inc->index->xxxxxxxxxxxxxxxxx';

?>
